<!--
 * @Author: CNN 969682281@qq.com
 * @Date: 2023-03-07 14:03:46
 * @LastEditors: CNN 969682281@qq.com
 * @LastEditTime: 2023-10-18 18:24:49
 * @FilePath: \newMould_BHZ\src\views\WaterManage\analyse\percentage.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <!-- 水稳---统计报表----产量统计 -->
  <div class="department" id="department">
    <!-- 左侧部门管理 -->
    <div class="structureTree" v-if="treelength">
      <div class="structureTree_title">
        <div>组织机构</div>
        <div>
          <!-- <el-checkbox v-model="checked" @change="see()">查看下级</el-checkbox> -->
        </div>
      </div>
      <tree :treeData="treeData" :checkData="checkData" nodeKey="fCompanyid" @getTreeInfo="handleNodeClick"
        :defaultProps="defaultProps">
      </tree>
    </div>
    <!-- 右侧 -->
    <div class="department_right">
      <!-- 机构名称 -->
      <el-breadcrumb :title="showName"></el-breadcrumb>
      <!-- 顶部导航栏 -->
      <div class="mechanism_nav">
        <!-- 左侧 -->
        <div class="mechanism_nav_left" style="width: 100%">
          <vDatePicker :defaultDate="value2" @selectPickDate="selectTime" width="215px !important">
          </vDatePicker>
          <el-select class="selectBhz" style="width: 160px" v-model="getList.stationId" placeholder="所有拌合站"
            @change="selectBhzFun">
            <el-option v-for="v in bhzDataList" :key="v.fItemName" :label="v.fItemName" :value="v.fItemValue">
            </el-option>
          </el-select>

          <el-select class="selectBhz" style="width: 160px" v-model="getList.machineId" placeholder="所有拌合机"
            @change="yj_searchForm(getList)">
            <el-option v-for="v in bhjDataList" :key="v.fItemName" :label="v.fItemName" :value="v.fItemValue">
            </el-option>
          </el-select>
        </div>
        <!-- 顶部按钮 -->
        <div class="print"></div>
      </div>

      <!-- 表格 -->
      <div class="text_table">
        <div class="materialTable" style="height: 130px">
          <tableData v-if="table_config.thead.length > 1" :config="table_config" :dataList="tableData"
            :loading="listLoading" @selection-change="selectionChange" @sort-change="sortChange" />
        </div>
        <div class="materialCharts">
          <myCharts :time1="this.value2[0]" :time2="this.value2[1]" :commonData="chartsData">
          </myCharts>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import vDatePicker from "@/components/public/v-DatePicker";
import tree from "@/components/public/waterManage_tree"; //左侧机构树
import TableData from "@/components/public/el-table"; //主页面列表
import elBreadcrumb from "@/components/public/el-breadcrumb.vue"; //面包屑

import myCharts from "@/components/waterManage/dataAnalysisManage/waterOutputCharts";

import {
  getUserCompanyTreeList, //左侧机构树接口
  getWaterInfoYieldStatement, //查询
  getStationmachineList, //拌合站列表
  getMachineListByStationId, //拌和机
} from "@/api/request.js";

export default {
  name: "waterOutput",
  data() {
    return {
      name: "waterOutput", //按钮配置页面代号
      formData: {}, //新增编辑向后台传递对象
      bhzDataList: [], //拌合站
      bhjDataList: [], //拌合机
      value2: "", //主页面日期筛选绑定值

      // 列表配置项
      table_config: {
        imgKeyWord: "url",
        fileKeyWord: "url",
        fileName: "fFileName",
        dialogHeight: "",
        thead: [],
        isCheckBox: false,
        isIndex: false,
        isOperation: true,
        // 表格操作列
      },
      tableData: [], //表格列表数据列表
      chartsData: {}, //echarts数据
      highlightBd: true, //左侧机构树停留背景色

      treelength: false, //左侧树显示隐藏
      checked: true, //查看下级按钮
      showName: "未选择公司",

      fId: null, //点击当前列表数据fId
      listLoading: false, //列表数据加载loading
      // 列表给后端传参
      getList: {
        fCompanyid: "", // 施工标段id，不为空
        startDates: "", // 开始时间
        endDates: "", // 截至时间
        stationId: "", // 拌合站id
        machineId: "", // 拌合机id
        buildPart: "", // 施工部位
        page: "", // 当前页数
        pageSize: "", // 每页显示条数
      },
      companyTree: "", //所有数据
      treeData: [], //左侧树状图数据

      // 左侧机构树
      defaultProps: {
        children: "children",
        label: "fShortname",
      },
      checkData: [],
      userInfo: {}, //登录用户
      treeText: {
        //树状图参数
        companyId: null,
        userId: null,
        text:null
      },
    };
  },
  components: {
    vDatePicker, //时间选择器
    tree, //左侧组织机构数
    TableData, //主页面列表
    elBreadcrumb, //面包屑

    myCharts,
  },
  mounted() {
    this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));

    if (JSON.parse(sessionStorage.getItem("waterManage_getLists"))) {
      this.getList = JSON.parse(sessionStorage.getItem("waterManage_getLists"));
      this.getbhjDataMethods(this.getList.stationId);
    }
    if (JSON.parse(sessionStorage.getItem("waterManage_treeCheck"))) {
      var id = JSON.parse(sessionStorage.getItem("waterManage_treeCheck"));
      this.checkData = [id.fCompanyid];
    }
    this.getTree();
    this.getDate();
    // this.btnRole(this.name);
  },

  methods: {
    // btnRole(name) {
    //   // 按钮权限
    //   var btnList = [];
    //   var ModuleList = JSON.parse(sessionStorage.getItem("ModuleList"));
    //   if (ModuleList.length > 0) {
    //     ModuleList.map((item) => {
    //       if (item.fEncode == name) {
    //         btnList = item.modulebuttonList;
    //       }
    //     });
    //     var btnStr = [];
    //     btnList.map((item) => {
    //       btnStr.push(item.fEncode);
    //     });
    //     // this.yj_view = btnStr.includes("yj_view");
    //   } else {
    //     // this.yj_view = true;
    //   }
    // },

    // 左侧树结构
    getTree() {
      this.treeText = {
        companyId: this.userInfo.companyId,
        userId: this.userInfo.userId,
        text: "2", // 0其他  1 砼拌合站 2水稳 3沥青
      };
      getUserCompanyTreeList(this.treeText)
        .then((res) => {
          if (res.errcode == 0) {
            var companyTree = res.data.userCompanyTreeList;
            this.treeData = this.commonJs.tranListToTreeData(companyTree);
            if (this.treeData[0].children == undefined) {
              this.treelength = false; //左侧组织机构是否显示
            } else {
              this.treelength = true;
            }
            if (this.checkData[0]) {
              this.showName = JSON.parse(
                sessionStorage.getItem("waterManage_treeCheck")
              ).fShortname;

              this.getList.fCompanyid = this.checkData[0];
            } else {
              this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
              this.checkData[0] = this.treeData[0].fCompanyid;
              this.getList.fCompanyid = this.treeData[0].fCompanyid; //当前点击组织机构ID
            }
            this.mixingPlantList("0", this.getList);
            this.yj_searchForm(this.getList); // 调用主页面列表接口
            // 列表
          } else {
            this.$message.error(res.msg);
            // this.$message.error("获取组织机构失败");
            return false;
          }
        })
        .catch(() => {
          return false;
        });
    },

    // 点击左侧机构树
    handleNodeClick(data) {
      this.showName = data.fShortname; //顶部显示机构名
      this.getList.fCompanyid = data.fCompanyid;
      this.mixingPlantList("0", this.getList);

      // this.getList.startDates = this.common.getDay(-6) + " " + "00:00:00"; // 开始时间
      // this.getList.endDates =
      //   this.common.getDay(0) + " " + "23:59:59"; // 截至时间
      this.getList.stationId = ""; //拌合站编号
      this.getList.machineId = ""; // 拌和机编号
      // this.getList.buildPart = ""; // 施工部位
      // this.value2 = [
      //   this.common.getDay(-6) + " " + "00:00:00",
      //   this.common.getDay(0) + " " + "23:59:59",
      // ];

      this.yj_searchForm(this.getList);
    },
    selectBhzFun(e) {
      this.getList.machineId = "";
      this.getbhjDataMethods(e);
    },
    getbhjDataMethods(data) {
      getMachineListByStationId({ stationId: data })
        .then((res) => {
          if (res.errcode == 0) {
            var arr = res.data.stationmachineList;
            this.bhjDataList = [
              {
                fItemName: "所有拌和机",
                fItemValue: "",
              },
            ];
            arr.map((item) => {
              this.bhjDataList.push({
                fItemName: item.fMachinename,
                fItemValue: item.fMachineid,
              });
            });
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 获取列表数据
    yj_searchForm(data) {
      this.listLoading = true;
      if (this.getList.buildPart == "施工部位") {
        data.buildPart = "";
      }
      if (this.getList.machineId == "所有拌合机") {
        data.machineId = "";
      }
      if (this.getList.stationId == "所有拌合站") {
        data.stationId = "";
      }
      window.sessionStorage.setItem(
        "waterManage_getLists",
        JSON.stringify(data)
      );

      getWaterInfoYieldStatement(data)
        .then((res) => {
          if (res.errcode == 0) {
            console.log("res", res);
            var that = this;
            that.listLoading = false;
            var StrengthGrade = []; //施工部位
            var ActualCalculation = []; //次数
            var DiskNumber = []; //  产量(t)

            var tableData = [{}, {}];
            var data = res.data;
            data.map((item) => {
              //数据二次处理
              StrengthGrade.push(item.F_BuildPart); //施工部位
              ActualCalculation.push(item.produceYield); // 产量(t)
              DiskNumber.push(item.totalCount); //次数
            });
            // 图表数据
            this.chartsData = {
              StrengthGrade: StrengthGrade,
              ActualCalculation: ActualCalculation,
              DiskNumber: DiskNumber,
            };
            console.log(" this.chartsData ", this.chartsData);
            this.table_config.thead = [
              {
                label: "施工部位	",
                prop: "type",
                minWidth: "100px",
                align: "center",
              },
            ];
            StrengthGrade.map((item, index) => {
              this.table_config.thead.push({
                label: item,
                prop: item,
                minWidth: "100px",
                align: "center",
              });
              tableData[0].type = "次数";
              tableData[0][item] = DiskNumber[index];
              tableData[1].type = "产量(t)	";
              tableData[1][item] = ActualCalculation[index];
            });

            that.tableData = tableData;
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          this.$message.error(error.msg);
        });
    },

    // 获取拌合站数据
    mixingPlantList(num, datas) {
      var data = {
        companyId: datas.fCompanyid, // 施工标段id
        modelType: num, // 类型
        stationType: "2", // 拌合站类型 1砼，2水稳，3沥青
      };
      getStationmachineList(data)
        .then((res) => {
          if (res.errcode == 0) {
            var arr = res.data.stationmachineList;

            this.bhzDataList = [
              {
                fItemName: "所有拌合站",
                fItemValue: "",
              },
            ];

            arr.map((item) => {
              this.bhzDataList.push({
                fItemName: item.fMachinename,
                fItemValue: item.fMachineid,
              });
            });
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          this.$message.error(error.msg);
        });
    },
    // 时间间隔
    // selectTime(e) {
    //   this.getList.startDates = e[0] + " " + "00:00:00"; // 开始时间
    //   this.getList.endDates = e[1] + " " + "23:59:59"; // 截至时间
    //   this.xlsxName = e[0] + "至" + e[1] + "水稳拌和站记录数据";

    //   this.yj_searchForm(this.getList);
    // },
    selectTime(e) {

      if (e[0] != '' && e[1] != '') {
        this.getList.startDates = e[0] + " " + "00:00:00"; // 开始时间
        this.getList.endDates = e[1] + " " + "23:59:59"; // 截至时间

      } else {

        this.getList.startDates = ""; // 开始时间
        this.getList.endDates = ""; // 截至时间

      }
      this.yj_searchForm(this.getList);
    },

    getDate() {
      if (JSON.parse(sessionStorage.getItem("waterManage_getLists"))) {
        var dataList = JSON.parse(
          sessionStorage.getItem("waterManage_getLists")
        );
        this.getList = {
          fCompanyid: dataList.fCompanyid, // 施工标段id，不为空
          startDates: dataList.startDates, // 开始时间
          endDates: dataList.endDates, // 截至时间
          stationId: dataList.stationId, // 拌合站id
          machineId: dataList.machineId, // 拌合机id
          buildPart: dataList.buildPart, // 施工部位
          page: "", // 当前页数
          pageSize: "", // 每页显示条数
        };
        this.value2 = [this.getList.startDates, this.getList.endDates];
        this.xlsxName =
          this.getList.startDates +
          "至" +
          this.getList.endDates +
          "水稳拌和站记录数据";
      } else {
        this.getList.startDates = this.common.getDay(-6) + " " + "00:00:00"; // 开始时间
        this.getList.endDates =
          this.common.getDay(0) + " " + "23:59:59"; // 截至时间

        this.xlsxName =
          this.common.getDay(-6) +
          "至" +
          this.common.getDay(0) +
          " " +
          "23:59:59" +
          "水稳拌和站记录数据";

        this.value2 = [
          this.common.getDay(-6) + " " + "00:00:00",
          this.common.getDay(0) + " " + "23:59:59",
        ];
      }
    },
    // 多选方法
    selectionChange() { },
    // 排序方法
    sortChange() { },
  },
};
</script>

<style scoped>
.printTables {
  background: #fff;
  display: flex;
  justify-content: end;
  padding: 5px;
  margin-top: 10px;
}

.dialogBody {
  padding: 3px 20px;
  height: 80%;
  overflow-y: auto;
}

.baseTitle {
  /* border: 1px solid #0c204d; */
  border-bottom: 1px solid #0c204d;

  height: 30px;
  line-height: 30px;
  position: relative;
  padding-left: 15px;
}

.baseTitle::after {
  display: block;
  content: "";
  height: 30px;
  width: 5px;
  position: absolute;
  left: 0;
  top: 0;
  background: #0c204d;
}

.materialCharts {
  height: calc(100% - 110px);
  width: 100%;
}
</style>
